package com.supor.aiot.mine.ui.login;

import android.annotation.SuppressLint;
import android.app.Application;

import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.Util;
import com.android.baseconfig.data.http.retrofit2.exception.ApiException;
import com.supor.aiot.common.app.api.UserImpl;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.R;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

public class LoginViewModel extends BaseViewModel {

    private MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
    private MutableLiveData<Result> loginResult = new MutableLiveData<>();
    private MutableLiveData<Result> sendCaptchaResult = new MutableLiveData<>();

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    LiveData<LoginFormState> getLoginFormState() {
        return loginFormState;
    }

    LiveData<Result> getLoginResult() {
        return loginResult;
    }

    public MutableLiveData<Result> getSendCaptchaResult() {
        return sendCaptchaResult;
    }

    @SuppressLint("CheckResult")
    public void login(String mobile, String captcha) {
        // can be launched in a separate asynchronous job
        // "15068999663", "1234", "0"
        mobile = mobile.replace(" ", "");
        Logc.e("mobile: " + mobile);
        UserImpl.getInstance().login(mobile, captcha, "0")
                .subscribe(apiResult -> {
                    Logc.i("accept: " + apiResult.toString());
                    loginResult.setValue(new Result.Success<>(apiResult.data));
                }, throwable -> {
                    Logc.i("accept: " + throwable);
                    if (throwable instanceof ApiException) {
                        ApiException exception = (ApiException) throwable;
                        loginResult.setValue(new Result.Error(exception));
                    } else {
                        loginResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void loginDataChanged(String username, String password) {
        if (!Util.isMobileNO(username)) {
            loginFormState.setValue(new LoginFormState(R.string.invalid_username, null));
        } else if (!isPasswordValid(password)) {
            loginFormState.setValue(new LoginFormState(null, R.string.invalid_password));
        } else {
            loginFormState.setValue(new LoginFormState(true));
        }
    }

    // A placeholder password validation check
    private boolean isPasswordValid(String password) {
        return password != null && password.trim().length() > 5;
    }

    @SuppressLint("CheckResult")
    public void sendCaptcha(String mobile) {
        // can be launched in a separate asynchronous job
        mobile = mobile.replace(" ", "");
        Map<String, String> object = new HashMap<>();
        object.put("mobileId", mobile);
        object.put("providerId", "0");
        UserImpl.getInstance().sendCaptcha(object)
                .subscribe(apiResult -> {
                    if (apiResult.isSuccess()) {
                        sendCaptchaResult.setValue(new Result.Success<>(apiResult.data));
                    } else {
                        sendCaptchaResult.setValue(new Result.Error(new ApiException(apiResult.status, apiResult.message)));
                    }
                }, throwable -> {
                    Logc.e("accept: " + throwable);
                    sendCaptchaResult.setValue(new Result.Error(new ApiException(throwable)));
                });
    }
}